﻿var keyValue = request('keyValue');

var bootstrap = function ($, learun) {
    "use strict";

    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        bind: function () {
            $('#files').lrUploader({isUpload: false});
        },
        initData: function () {
            if (!!keyValue) {
                $.lrSetForm(config.base_server() + 'emails/findById?id=' + keyValue + '&access_token=' + config.getToken().access_token, function (data) {//
                    console.log(data);
                    $('#theme').val(data.theme);
                    $('#senderName').val(data.senderUsername);

                    $('#files').lrUploaderSet(data.files);
                    $('#senderTime').val(learun.formatDate(data.senderTime, 'yyyy-MM-dd hh:mm:ss'));
                    var loginInfo = learun.clientdata.get(['userinfo']);
                    if (data.bccsend.indexOf(loginInfo.userId) == -1) {
                        learun.clientdata.getAsync('user', {
                            key: data.address,
                            callback: function (name) {
                                $('#addresss').val(name);
                            }
                        });
                        if (data.copysend == '') {
                            $('#copysend').parent().remove();
                        }
                        learun.clientdata.getAsync('user', {
                            key: data.copysend,
                            callback: function (name) {
                                $('#copysend').val(name);
                            }
                        });
                    } else {
                        $('#copysend').parent().remove();
                        $('#address').val(loginInfo.realName);
                    }

                    var arrEntities = {'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"'};
                    var str = data.content.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
                        return arrEntities[t];
                    });
                    $('#content').html(str);
                });
            }
        }
    };
    page.init();
}